/**
 * Stage类，不能实例化
 * @author zzhanglin
 */
Stage = function()
{
	DisplayObjectContainer.call(this);
	this.className="Stage" ;
	
	this._frameRate = 25 ;
	this._stageWidth=1 ;
	this._stageHeight=1 ;
	this._context ; //context2D
	this._isRun = false ; //系统是否运行
	this._gameLoopTimeId =0  ;
	this._runTime = 0; //计时器，系统运行的时间
	this._buttonDown = false ; //鼠标是否按下
};
FLASH.extend(Stage,DisplayObjectContainer); //继承自DisplayObjectContainer

//===============================================
//================gettter/setter======================
Stage.prototype.getContext = function(){ return this._context ;} ;
Stage.prototype.getStageWidth = function(){ return this._stageWidth ; };
Stage.prototype.getStageHeight = function(){ return this._stageHeight ; };

Stage.prototype.setFrameRate = function( value ){ 
	this._frameRate = value ;
	this.pause();
	this.start();
};
Stage.prototype.getFrameRate = function(){ return this._frameRate ; };
//===============================================

/**
 * 开始运行
 */
Stage.prototype.start = function()
{
	if(this._isRun) return ;
	
	var _self = this ;
	//主循环
	this._gameLoopTimeId = setInterval( loop , (1000/this._frameRate)>>0 );
	function loop()
	{ 
		if(_self._isRun){
			_self._runTime++ ;
			_self._context.clearRect(0, 0, _self._stageWidth , _self._stageHeight );
		}
		 _self._gameLoop(1 , _self._context , false ) ;
	};
	this._isRun = true ;
};
/**
 * 停止
 */
Stage.prototype.pause = function()
{
	clearInterval(this._gameLoopTimeId);
	this._isRun = false ;
};
/**
 * 休眠
 */
Stage.prototype.sleep = function()
{
	this._isRun = false ;
};
/**
 * 恢复运行
 */
Stage.prototype.wakeUp = function()
{
	this._isRun = true ;
};

//===========stage不需要执行的方法============================

Stage.prototype.hitTestPoint = function( x , y) { return false; };

//======================================================

//========私有方法===============================
/**
 * 渲染图形
 *@param globalAlpha 全局透明度
 *@param context 
 *@param parentMatrixchanged
 */
Stage.prototype._render = function( globalAlpha , context , parentMatrixChanged )
{ };

//鼠标事件 ===========================================

/**
 * 鼠标移动
 * @param localX 相对canvas的鼠标坐标位置 
 * @param localY 相对canvas的鼠标坐标位置 
 */
Stage.prototype._onMouseMove = function( localX, localY, ctrlKey, altKey,shiftKey, buttonDown, delta )
{
	DisplayObjectContainer.prototype._onMouseMove.call(this, localX, localY, ctrlKey, altKey,shiftKey, this._buttonDown, delta );
	this._mouseX = localX ;
	this._mouseY = localY ;
	if(this.hasEventListener(MouseEvent.MOUSE_MOVE))
	{
		var evt = new MouseEvent( MouseEvent.MOUSE_MOVE,true, localX  , localY , 
				ctrlKey, altKey,shiftKey, this._buttonDown, delta );
		this.dispatchEvent(evt);
	}
	this._mouseIsOver = true ;
	return true;
};
/**
 * 鼠标按下
 */
Stage.prototype._onMouseDown = function( ctrlKey, altKey,shiftKey, buttonDown, delta )
{
	this._buttonDown = true ;
	DisplayObjectContainer.prototype._onMouseDown.call(this, ctrlKey, altKey,shiftKey, true , delta );
	if(this.hasEventListener(MouseEvent.MOUSE_DOWN))
	{
		var evt = new MouseEvent( MouseEvent.MOUSE_DOWN, true, ctrlKey, altKey,shiftKey, true , delta );
		this.dispatchEvent(evt);
	}
	return true;
};
/**
 * 鼠标弹起
 */
Stage.prototype._onMouseUp = function( ctrlKey, altKey,shiftKey, buttonDown, delta )
{
	this._buttonDown = false ;
	DisplayObjectContainer.prototype._onMouseUp.call(this, ctrlKey, altKey,shiftKey, false , delta );
	if(this.hasEventListener(MouseEvent.MOUSE_UP))
	{
		var evt = new MouseEvent( MouseEvent.MOUSE_UP,true ,  ctrlKey, altKey,shiftKey, false, delta );
		this.dispatchEvent(evt);
	}
	this._onMouseClick(ctrlKey, altKey,shiftKey, false, delta);
	return true;
};
/**
 * 鼠标单击
 */
Stage.prototype._onMouseClick = function( ctrlKey, altKey,shiftKey, buttonDown, delta )
{
	if(this.hasEventListener(MouseEvent.CLICK))
	{
		var evt = new MouseEvent( MouseEvent.CLICK,true, ctrlKey, altKey,shiftKey, this._buttonDown, delta );
		this.dispatchEvent(evt);
	}
	return true;
};
/**
 * 鼠标在上面
 */
Stage.prototype._onMouseOver= function( ctrlKey, altKey,shiftKey, buttonDown, delta )
{ };
/**
 * 鼠标移出此对象
 */
Stage.prototype._onMouseOut = function( ctrlKey, altKey,shiftKey, buttonDown, delta )
{ };

/**
 * 键盘按下
 */
Stage.prototype._onKeyDown = function(charCode, keyCode, keyLocation, ctrlKey, altKey, shiftKey)
{
	if(this.hasEventListener( KeyboardEvent.KEY_DOWN ))
	{
		var evt = new KeyboardEvent( KeyboardEvent.KEY_DOWN , false ,  charCode, keyCode, keyLocation, ctrlKey, altKey, shiftKey );
		this.dispatchEvent(evt);
	}
};

/**
 * 按健弹起
 */
Stage.prototype._onKeyUp = function(charCode, keyCode, keyLocation, ctrlKey, altKey, shiftKey)
{
	if(this.hasEventListener( KeyboardEvent.KEY_UP))
	{
		var evt = new KeyboardEvent( KeyboardEvent.KEY_UP , false ,  charCode, keyCode, keyLocation, ctrlKey, altKey, shiftKey );
		this.dispatchEvent(evt);
	}
};